package com.zzy.database.controller;

import com.zzy.database.pojo.Branch;
import com.zzy.database.pojo.Customer;
import com.zzy.database.pojo.Result;
import com.zzy.database.service.BranchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/branches")
public class BranchController {
    @Autowired
    private BranchService branchService;

    @PostMapping
    public Result addBranch(@RequestBody Branch branch) {
        try{
            branch.setBranch_id(UUID.randomUUID().toString());
            branchService.addBranch(branch);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping
    public Result deleteBranchById(@RequestBody Branch branch) {
        try {
            branchService.deleteBranchById(branch.getBranch_id());
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @PutMapping
    public Result updateBranch(@RequestBody Branch branch) {
        try {
            branchService.updateBranch(branch);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/getById")
    public Result getBranchById(@RequestBody Branch branch) {
        Branch tempBranch = branchService.getBranchById(branch.getBranch_id());
        return Result.success(tempBranch);
    }

    @GetMapping("/getAll")
    public Result getAllBranch() {
        System.out.println("ssss");
        List<Branch> branchList = branchService.getAllBranches();
        return Result.success(branchList);
    }



}
